package com.cheyun.netsalev3.view.aftersale

import android.content.Context
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.databinding.ActivityAfterSaleDriveBinding
import com.cheyun.netsalev3.utils.BaseActivity


class AfterSaleDriveActivity : BaseActivity<ActivityAfterSaleDriveBinding>() {

    override fun initContentView(): Int {
        return R.layout.activity_after_sale_drive
    }


    private var searchMap = HashMap<String, String>()

    private var listFragmet = ArrayList<Fragment>()

    override fun initData() {
    }

    override fun initView() {
        listFragmet.add(AftersaleDriveFragment())
        val pagerAdapter = HomePageAdapter(supportFragmentManager, listFragmet)

        binding.viewPager.adapter = pagerAdapter
//        binding.tabLayout.setupWithViewPager(binding.viewPager)

        binding.searchText.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
                //关闭软键盘
                closeKeyword()
                //do something
                //doSearch();
                searchMap = HashMap<String, String>()
                searchMap["keyword"] = binding.searchText.text.toString()

                refresh()
                true
            } else
                false
        }
        binding.leftBut.setOnClickListener{
            finish()
        }
    }

    fun closeKeyword(){
        val view = window.peekDecorView()
        if (view != null) {
            val inputmanger = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputmanger.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }

    fun refresh(){
        var hashMap = HashMap<String, String>()

        searchMap.forEach {
            hashMap[it.key] = it.value
        }
        (listFragmet[0] as AftersaleDriveFragment).setSearch(hashMap)

    }
}
